package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.knowledge.domain.SysPortalImages;
import cn.cdeden.knowledge.service.SysPortalImagesService;
import cn.cdeden.knowledge.service.dto.SysPortalImagesDto;
import cn.cdeden.knowledge.service.dto.SysPortalImagesQueryParam;
import cn.cdeden.knowledge.service.mapper.SysPortalImagesMapper;
import cn.cdeden.knowledge.utils.PageUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author gjx
* @date 2024-09-26
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = SysPortalImagesService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysPortalImagesServiceImpl extends ServiceImpl<SysPortalImagesMapper, SysPortalImages> implements SysPortalImagesService {

    // private final RedisUtils redisUtils;
    private final SysPortalImagesMapper sysPortalImagesMapper;

    @Override
    public TableDataInfo<SysPortalImagesDto> queryAll(SysPortalImagesQueryParam query, Pageable pageable) {
        IPage<SysPortalImages> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<SysPortalImages> page = sysPortalImagesMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));

        return MapstructUtils.convertTableDataInfo(TableDataInfo.build(page), SysPortalImagesDto.class);
    }

    @Override
    public List<SysPortalImagesDto> queryAll(SysPortalImagesQueryParam query){
        return MapstructUtils.convert(sysPortalImagesMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), SysPortalImagesDto.class);
    }

    @Override
    public SysPortalImages getById(Long id) {
        return sysPortalImagesMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public SysPortalImagesDto findById(Long id) {
        return MapstructUtils.convert(getById(id), SysPortalImagesDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(SysPortalImagesDto resources) {
        SysPortalImages entity = MapstructUtils.convert(resources, SysPortalImages.class);
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(SysPortalImagesDto resources){
        SysPortalImages entity = MapstructUtils.convert(resources, SysPortalImages.class);
        boolean ret = this.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Set<Long> ids){
        // delCaches(ids);
        return this.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.deleteByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<SysPortalImagesDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (SysPortalImagesDto sysPortalImages : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("名称", sysPortalImages.getName());
              map.put("图片地址", sysPortalImages.getUrl());
              map.put("创建时间", sysPortalImages.getCreateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
